package com.macro.mall.controller;


import com.macro.mall.common.api.CommonResult;
import com.macro.mall.entity.Credit;
import com.macro.mall.service.CreditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 征信查询表 前端控制器
 *
 * @since 2021-07-04
 */
@RestController
@RequestMapping("/credit")
@Api(tags = "CreditController", description = "征信查询")
public class CreditController {

    @Autowired
    private CreditService creditService;

    @ApiOperation("分页征信查询记录")
    @RequestMapping(value = "/selectList", method = RequestMethod.POST)
    public CommonResult selectList(String name, Integer page, Integer size) {
        List<Credit> creditList = null;
        try {
            creditList= creditService.find(name, page, size);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success(creditList);
    }

    @ApiOperation("添加征信查询")
    @RequestMapping(value = "/addOne", method = RequestMethod.POST)
    public CommonResult addOne(@RequestBody Credit credit) {
        try {
            creditService.saveNotNull(credit);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("添加成功");
    }

    @ApiOperation("删除征信查询")
    @RequestMapping(value = "/delOne", method = RequestMethod.POST)
    public CommonResult delOne(Integer id) {
        try {
            creditService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("删除成功");
    }

    @ApiOperation("修改征信查询")
    @RequestMapping(value = "/upDataOne", method = RequestMethod.POST)
    public CommonResult upDataOne(@RequestBody Credit credit) {
        try {
            creditService.updateNotNullById(credit);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
        return CommonResult.success("修改成功");
    }
}
